package com.example.bootdemo.configuration;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfiguration {

    public static final String DIRECT_EXCHANGE_NAME = "directExchange";

    public static final String TOPIC_EXCHANGE_NAME = "topicExchange";

    public static final String DIRECT_QUEUE_NAME = "directQueue";

    public static final String XIAOMI_QUEUE = "xiaomiQueue";

    public static final String HUAWEI_QUEUE = "huaweiQueue";

    public static final String IPHONE_QUEUE = "iphoneQueue";

    public static final String DIRECT_KEY = "direct.key";

    public static final String XIAOMI_KEY = "xiaomi.#";

    public static final String HUAWEI_KEY = "huawei.#";

    public static final String IPHONE_KEY = "#.iphone.#";




    @Bean
    @Qualifier
    public DirectExchange directExchange(){
        return new DirectExchange(DIRECT_EXCHANGE_NAME);
    }

    @Bean
    public TopicExchange topicExchange(){
        //参数列表：交换器名字、重启后是否有效、长期未使用是否删除
        return new TopicExchange(TOPIC_EXCHANGE_NAME,true,false);
    }

    @Bean
    @Qualifier
    public Queue directQueue(){
        //第一个参数为name,第二个参数为是否持久化
        return new Queue(DIRECT_QUEUE_NAME,true);
    }

    @Bean
    public Queue topicQueue1(){

        return new Queue(XIAOMI_QUEUE,true);
    }

    @Bean
    public Queue topicQueue2(){
        return new Queue(HUAWEI_QUEUE,true);
    }

    @Bean
    public Queue topicQueue3(){
        return new Queue(IPHONE_QUEUE,true);
    }

    @Bean
    public Binding binding(){
        //绑定一个队列 to: 绑定到哪个交换机上面 with：绑定的路由建（routingKey）
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_KEY);
    }

    @Bean
    public Binding xiaomiBinding(){
        return BindingBuilder.bind(topicExchange()).to(topicExchange()).with(XIAOMI_KEY);
    }

    @Bean
    public Binding huaweiBinding(){
        return BindingBuilder.bind(topicExchange()).to(topicExchange()).with(HUAWEI_KEY);
    }

    @Bean
    public Binding iphoneBinding(){

        return BindingBuilder.bind(topicExchange()).to(topicExchange()).with(IPHONE_KEY);
    }



}
